// 医院地点
createDropdown({
    selectElement: document.querySelector('.location-select'),
    selectedElement: document.querySelector('.location-selected'),
    selectedTextElement: document.querySelector('.location-selected-text'),
    optionsContainer: document.querySelector('.location-options'),
    optionsElements: document.querySelectorAll('.location-option') ? document.querySelectorAll('.location-option') : null,
    arrowElement: document.getElementById('location-arrow'),
    closestItem: '.location-select',
    defaultText: '本院'
});

// 科室 
createDropdown({
    selectElement: document.querySelector('.department-select'),
    selectedElement: document.querySelector('.department-selected'),
    selectedTextElement: document.querySelector('.department-selected-text'),
    optionsContainer: document.querySelector('.department-options'),
    optionsElements: document.querySelectorAll('.department-option') ? document.querySelectorAll('.department-option') : null,
    arrowElement: document.getElementById('department-arrow'),
    closestItem: '.department-select',
    defaultText: '医务科'
});

// 科室门号
createDropdown({
    selectElement: document.querySelector('.department-number-select'),
    selectedElement: document.querySelector('.department-number-selected'),
    selectedTextElement: document.querySelector('.department-number-selected-text'),
    optionsContainer: document.querySelector('.department-number-options'),
    optionsElements: document.querySelectorAll('.department-number-option') ? document.querySelectorAll('.department-number-option') : null,
    arrowElement: document.getElementById('department-number-arrow'),
    closestItem: '.department-number-select',
    defaultText: '测试科室2(门诊)'
});

// 主执行逻辑
async function initializeOptions() {
    
    try {
        let pidDefault = 0;
        // 获取 locationData
        const locationResult = await fetchData(`${baseUrl}/api/sysOrga/all4Login`, 'POST', { pid: pidDefault });
        const locationData = locationResult.result.map(item => ({
            pid: item.pid,
            name: item.name
        }));
        createOptions('.location-options', locationData, 'location');

        // 获取 departmentData
        const firstLocationId = locationResult.result[0].id;
        const departmentResult = await fetchData(`${baseUrl}/api/sysOrga/all4Login`, 'POST', {
            pid: firstLocationId,
            type: getRadioText()
        });
        const departmentData = departmentResult.result.map(item => ({
            pid: item.pid,
            name: item.name
        }));
        createOptions('.department-number-options', departmentData, 'department-number');
        
        const customRadios = document.querySelectorAll('.custom-radio')

        customRadios.forEach(option => {
            option.addEventListener('click', async() => {
                // 获取选中 .custom-radio 中的文字内容
                let selectedText = option.querySelector('span').textContent;
                
                const newDepartmentResult = await fetchData(`${baseUrl}/api/sysOrga/all4Login`, 'POST', {
                    pid: firstLocationId,
                    type: selectedText
                });
                
                const newDepartmentData = newDepartmentResult.result.map(item => ({
                    pid: item.pid,
                    name: item.name
                }));
                
                createOptions('.department-number-options', newDepartmentData, 'department-number');
                
                // 科室门号
                createDropdown({
                    selectElement: document.querySelector('.department-number-select'),
                    selectedElement: document.querySelector('.department-number-selected'),
                    selectedTextElement: document.querySelector('.department-number-selected-text'),
                    optionsContainer: document.querySelector('.department-number-options'),
                    optionsElements: document.querySelectorAll('.department-number-option') ? document.querySelectorAll('.department-number-option') : null,
                    arrowElement: document.getElementById('department-number-arrow'),
                    closestItem: '.department-number-select',
                    defaultText: '选择科室'
                });
            })
        })

    } catch (error) {
        // 错误处理
        console.error("Error initializing options:", error);
    }
}

initializeOptions();








